
#import "SceneGameOver.h"
#import "SceneMenu.h"
#import "SceneBatteryView.h"
#import "global.h"
#import "OpenFeint.h"


@implementation GameOver

+(id) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	GameOver *layer = [GameOver node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if ( (self=[super init] )) 
	{
		m_windowSize = [[CCDirector sharedDirector] winSize];
		
		if ( m_windowSize.width == 768 )
			m_DeviceKind = DEVICE_IPAD;
		else
			m_DeviceKind = DEVICE_IPHONE;
		
		//bg image
		if ( m_DeviceKind == DEVICE_IPHONE )
			spBackground = [CCSprite spriteWithFile:@"background1.png"];
		else
			spBackground = [CCSprite spriteWithFile:@"background1_pad.png"];
		[spBackground setPosition:ccp(CX,CY)];
		[self addChild:spBackground];
		
		//bg sub image
		spBall1 = [CCSprite spriteWithFile: @"ball.png"];
		[spBall1 setScale:0.7f];
		[spBall1 setPosition: ccp(CX-80,CY+22)];
		[self addChild:spBall1];
		spBall2 = [CCSprite spriteWithFile: @"ball.png"];
		[spBall2 setScale:0.7f];
		[spBall2 setPosition: ccp(CX-83,CY-20)];
		[self addChild:spBall2];
		spBall3 = [CCSprite spriteWithFile: @"ball.png"];
		[spBall3 setScale:0.7f];
		[spBall3 setPosition: ccp(CX-55,CY-60)];
		[self addChild:spBall3];
		
		id rotate1 = [CCRotateBy actionWithDuration:1 angle:360];
		id repeat1 = [CCRepeatForever actionWithAction:rotate1];
		[spBall1 runAction:repeat1];
		id rotate2 = [CCRotateBy actionWithDuration:1 angle:360];
		id repeat2 = [CCRepeatForever actionWithAction:rotate2];
		[spBall2 runAction:repeat2];	
		id rotate3 = [CCRotateBy actionWithDuration:1 angle:360];
		id repeat3 = [CCRepeatForever actionWithAction:rotate3];
		[spBall3 runAction:repeat3];	
		
		CCSprite *spWhiteboard = [CCSprite spriteWithFile:@"whiteboard.png"];
		[spWhiteboard setScale:0.5];
		[spWhiteboard setPosition:ccp(CX,CY+120)];
		[self addChild:spWhiteboard];
		
		currentBatter = [[batter alloc] init];
		if ( m_DeviceKind == DEVICE_IPHONE )
			currentBatter.spBatterReady.position = ccp(BAT_START_X-10, BAT_START_Y-40);
		else
			currentBatter.spBatterReady.position = ccp(BAT_START_X-10, BAT_START_Y+200);
		[self addChild:currentBatter.spBatterReady];
		[self addChild:currentBatter.spBatterSwing];
		[currentBatter.spBatterSwing setVisible: FALSE];
		[currentBatter.spBatterReady runAction:[CCRepeatForever actionWithAction: currentBatter.aniBatterReady]];
				
		//score
		CCLabel* labelScoreTitle = [CCLabel labelWithString:@"score:" fontName:@"MarkerFelt-Thin" fontSize:20];
		[self addChild: labelScoreTitle];
		[labelScoreTitle setColor:ccc3(0,0,0)];
		[labelScoreTitle setPosition: ccp(CX, CY+160)];
		
		NSString* strScore = [NSString stringWithFormat:@"%d", gameInfo.nScore];
		CCLabel* labelScore = [CCLabel labelWithString:strScore fontName:@"MarkerFelt-Thin" fontSize:20];
		[self addChild: labelScore];
		[labelScore setColor:ccc3(0,0,0)];
		[labelScore setPosition: ccp(CX, CY+135)];
		
		//ranking
		int nRanking = 99;
		NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
		if ( [prefs boolForKey:@"ranking_0"] )
			gameInfo.nRanking[0] = [prefs integerForKey:@"ranking_0"];
		if ( [prefs boolForKey:@"ranking_1"] )
			gameInfo.nRanking[1] = [prefs integerForKey:@"ranking_1"];
		if ( [prefs boolForKey:@"ranking_2"] )
			gameInfo.nRanking[2] = [prefs integerForKey:@"ranking_2"];
		if ( [prefs boolForKey:@"ranking_3"] )
			gameInfo.nRanking[3] = [prefs integerForKey:@"ranking_3"];
		if ( [prefs boolForKey:@"ranking_4"] )
			gameInfo.nRanking[4] = [prefs integerForKey:@"ranking_4"];
		
		for ( int i=0; i<5; i++ )
		{
			if ( gameInfo.nScore >= gameInfo.nRanking[i] )
			{
				gameInfo.nRanking[i] = gameInfo.nScore;
				NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
				nRanking = i+1;
				
				switch ( i )
				{
					case 0 :
						if ( [prefs boolForKey:@"ranking_3"] )
							[prefs setInteger:[prefs integerForKey:@"ranking_3"] forKey:@"ranking_4"];
						
						if ( [prefs boolForKey:@"ranking_2"] )
							[prefs setInteger:[prefs integerForKey:@"ranking_2"] forKey:@"ranking_3"];
						
						if ( [prefs boolForKey:@"ranking_1"] )
							[prefs setInteger:[prefs integerForKey:@"ranking_1"] forKey:@"ranking_2"];
						
						if ( [prefs boolForKey:@"ranking_0"] )
							[prefs setInteger:[prefs integerForKey:@"ranking_0"] forKey:@"ranking_1"];
						
						[prefs setInteger:gameInfo.nScore forKey:@"ranking_0"];
						break;
					case 1 :
						if ( [prefs boolForKey:@"ranking_3"] )
							[prefs setInteger:[prefs integerForKey:@"ranking_3"] forKey:@"ranking_4"];
						
						if ( [prefs boolForKey:@"ranking_2"] )
							[prefs setInteger:[prefs integerForKey:@"ranking_2"] forKey:@"ranking_3"];
						
						if ( [prefs boolForKey:@"ranking_1"] )
							[prefs setInteger:[prefs integerForKey:@"ranking_1"] forKey:@"ranking_2"];	
						
						[prefs setInteger:gameInfo.nScore forKey:@"ranking_1"];
						break;
					case 2 :
						if ( [prefs boolForKey:@"ranking_3"] )
							[prefs setInteger:[prefs integerForKey:@"ranking_3"] forKey:@"ranking_4"];
						
						if ( [prefs boolForKey:@"ranking_2"] )
							[prefs setInteger:[prefs integerForKey:@"ranking_2"] forKey:@"ranking_3"];
						
						[prefs setInteger:gameInfo.nScore forKey:@"ranking_2"];
						break;
					case 3 :
						if ( [prefs boolForKey:@"ranking_3"] )
							[prefs setInteger:[prefs integerForKey:@"ranking_3"] forKey:@"ranking_4"];
						
						[prefs setInteger:gameInfo.nScore forKey:@"ranking_3"];
						break;
					case 4 :
						[prefs setInteger:gameInfo.nScore forKey:@"ranking_4"];
						break;
				}
				break;
			}
		}
		
		CCLabel* labelRankingTitle = [CCLabel labelWithString:@"rankig:" fontName:@"MarkerFelt-Thin" fontSize:20];
		[self addChild: labelRankingTitle];
		[labelRankingTitle setColor:ccc3(0,0,0)];
		[labelRankingTitle setPosition: ccp(CX, CY+100)];
		
		NSString *strRanking;
		if ( nRanking == 99 || gameInfo.nScore == 0 )
			strRanking = [NSString stringWithFormat:@"-", nRanking];
		else
			strRanking = [NSString stringWithFormat:@"#%d", nRanking];
		
		CCLabel* labelRanking = [CCLabel labelWithString:strRanking fontName:@"MarkerFelt-Thin" fontSize:20];
		[self addChild: labelRanking];
		[labelRanking setColor:ccc3(0,0,0)];
		[labelRanking setPosition: ccp(CX, CY+75)];
		
		//menu
		[CCMenuItemFont setFontSize:30];
        [CCMenuItemFont setFontName:@"MarkerFelt-Thin"];
		
        CCMenuItem *again = [CCMenuItemFont itemFromString:@"play again" target:self selector:@selector(playAgain:)];
		[((CCMenuItemFont *)again).label setColor:ccc3(0,0,0)];	
		[again setPosition:ccp(0, 25)];
		
		CCMenuItem *ranking = [CCMenuItemFont itemFromString:@"online rank" target:self selector:@selector(onlineRanking:)];
		[((CCMenuItemFont *)ranking).label setColor:ccc3(0,0,0)];	
		[ranking setPosition:ccp(0, -15)];
		
		CCMenuItem *gomenu = [CCMenuItemFont itemFromString:@"menu" target:self selector:@selector(gotoMenu:)];
		[((CCMenuItemFont *)gomenu).label setColor:ccc3(0,0,0)];	
		[gomenu setPosition:ccp(0, -55)];
		
		CCMenu *menu = [CCMenu menuWithItems:again, ranking, gomenu, nil];	
//		[menu alignItemsVertically];
        [self addChild:menu z:1];
		
		//etc
		isTouchEnabled = YES;
		
	}
	return self;
}

-(void)playAgain: (id)sender {
	BatteryView* gs = [BatteryView node];
    [[CCDirector sharedDirector] replaceScene: (CCScene*)gs];
}

-(void)onlineRanking: (id)sender {
	[OpenFeint launchDashboardWithHighscorePage:(NSString*)@"448694"];
}

-(void)gotoMenu: (id)sender {
    Menu *ms = [Menu node];
    [[CCDirector sharedDirector] replaceScene: (CCScene*)ms];
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
	UITouch* touch = [touches anyObject];
	CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:touch.view]];
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
//	[_session.delegates removeObject: self]; 
	[currentBatter.spBatterReady release];
	[currentBatter.aniBatterReady release];
	[currentBatter.spBatterSwing release];
	[currentBatter.aniBatterSwing release];
	[currentBatter release];
		
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end





